import axios from "axios"

export default {
  state () {
    return {
      list: []
    }
  },
  mutations: {
    setlist (state, list) {
      state.list = list
    },
    findlist(state,obj){
     const itemobj= state.list.find(item=>item.id==obj.id)
     itemobj.count = obj.count
    }
  },
  actions: {
    async setqing ({commit}) {
      const res = await axios.get('http://192.168.87.184:3000/cart')
      console.log(res);
      commit('setlist', res.data)
    },
    async changecount({commit},obj){
      const res = await axios.patch('http://192.168.87.184:3000/cart/'+obj.id,{
        count:obj.count
      })
      console.log(res);
      commit('findlist',obj)
    }
  },
  getters: {
    total(state){
      state.list.reduce((sum,item)=>sum+=item.count,0)
    },
    priceTotal(state){
      state.list.reduce((sum,item)=>sum+=item.count*item.price,0)
    }
  },
  namespaced: true
}